package com.sakura.mall.member.member.controller;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.common.response.R;
import com.sakura.mall.member.member.domain.entity.MemberEntity;
import com.sakura.mall.member.member.domain.request.AddMemberRequest;
import com.sakura.mall.member.member.domain.request.MemberPageRequest;
import com.sakura.mall.member.member.domain.request.UpdateMemberRequest;
import com.sakura.mall.member.member.domain.vo.MemberVo;
import com.sakura.mall.member.member.service.MemberService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 会员
 *
 * @author sakura
 */
@RestController
@RequestMapping("/member")
//@RequiredArgsConstructor
@RefreshScope
public class MemberController {

    private final MemberService memberService;

    @Value(value = "${app.name}")
    private String name;

    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    //    private final CouponFeignClient couponFeignClient;

    @GetMapping("test")
    public R<Object> test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("test");
        memberEntity.setCity(name);

//        R page = couponFeignClient.page();
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("page", page);
//        hashMap.put("member", memberEntity);

        return R.ok(memberEntity);
    }


    /**
     * 新建
     *
     * @return R {@link Void}
     */
    @PostMapping
    public R<Void> create(@RequestBody @Validated AddMemberRequest request) {
        memberService.add(request);
        return R.ok();
    }


    /**
     * 分页获取
     *
     * @return R {@link Page<MemberVo>}
     */
    @GetMapping("list")
    public R<Page<MemberVo>> page(MemberPageRequest request) {
        Page<MemberVo> ret = memberService.queryPage(request);
        return R.ok(ret);
    }


    /**
     * 根据id删除
     *
     * @return R {@link Void}
     */
    @DeleteMapping("{id}")
    public R<Void> remove(@PathVariable Long id) {
        memberService.removeById(id);
        return R.ok();
    }


    /**
     * 根据id更新
     *
     * @return R {@link Void}
     */
    @PostMapping("{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody UpdateMemberRequest request) {
        memberService.updateById(id, request);
        return R.ok();
    }

}
